package com.xh.easyretry.listener;

import com.xh.easyretry.annotations.XHRetry;
import com.xh.easyretry.common.XHRetryContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

import static com.xh.easyretry.common.XHRetryConstants.DEFAULT_START_RETRY_COUNT;

@Slf4j
public class FirstRetryListener<T extends XHRetryContext> extends RetryListener<T> {

    @Override
    public void listen(T t) {
        Integer currentRetryCount = t.getCurrentRetryCount();
        if (Objects.isNull(currentRetryCount) || !Objects.equals(currentRetryCount, DEFAULT_START_RETRY_COUNT)) {
            return;
        }
        XHRetry xhRetry = t.getXhRetry();
        if (Objects.isNull(xhRetry)) {
            throw new IllegalArgumentException("xhRetry is null");
        }
        String firstRetryListenerMethodName = xhRetry.firstRetryListener();
        doWithSpecifyMethod(t, firstRetryListenerMethodName);
    }

}
